﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Sales
{
    public partial class frmConfig : Form
    {
        public SysConfig config;
        public PrintConfig printConfig;
        private static frmSetPrint setPrint = null;
        private BasicBLL basicBLL = null;
        public frmConfig()
        {
            InitializeComponent();
            Init();


        }
        public void Init()
        {
            config = Common2.GetConfig();//获取相关配置属性


            this.ctxtAccount2.Text = config.SourceAccount;
            this.ctxtDatabaseName2.Text = config.SourceDatabaseName;
            this.ctxtPassWord2.Text = config.SourcePWD;
            this.ctxtServer2.Text = config.SourceServer;

            if (config.IsChoice > 0)
                this.radio1.Checked = true;
            else
                this.radio1.Checked = false;

            SetYyyType(config.EmployeeRate);
            SetXsType(config.SalesRate);
            SetSpdaType(config.SpdaRate);

            this.ctxtSysName.Text = config.SysName;
            this.ctxtUrl.Text = config.WebService;
            this.ctxtWebApi.Text = config.WebApi;

            //打印机配置 模块
            printConfig = Common2.GetPrintConfig();

            GetComPrintList();
            this.cCombox1.Text = printConfig.PortName;
            BindPrintList(printConfig.Ls);

        }

        private void BindPrintList(List<PrintItem> ls)
        {
            this.cDataGridView1.DataSource = ls;
            this.cDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private static frmConfig frmfileName = null;
        public static frmConfig CreateInstance()
        {
            if (null == frmfileName)
            {
                frmfileName = new frmConfig();
            }
            return frmfileName;
        }

        private void GetComPrintList()
        {

            //查找系统安装的打印机
            foreach (String pkInstalledPrinters in PrinterSettings.InstalledPrinters)
            {
                this.comPrint.Items.Add(pkInstalledPrinters);
            }
            this.comPrint.SelectedIndex = 0;
        }

        private void frmConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmfileName = null;
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            string account = this.ctxtAccount2.Text;
            string dataBase = this.ctxtDatabaseName2.Text;
            string passWord = this.ctxtPassWord2.Text;
            string server = this.ctxtServer2.Text;
            this.config.SourceAccount = account;
            this.config.SourceDatabaseName = dataBase;
            this.config.SourcePWD = passWord;
            this.config.SourceServer = server;

            Common2.SetConfig(config);
            MessageBox.Show("操作成功");
        }



        private int GetXsType()
        {
            int type = 0;
            if (this.rdxsType1.Checked)
                type = 1;
            else if (this.rdxsType2.Checked)
                type = 2;
            else if (this.rdxsType3.Checked)
                type = 3;
            else if (this.rdxsType4.Checked)
                type = 4;
            else if (this.rdxsType5.Checked)
                type = 5;
            return type;
        }
        private void SetXsType(int type)
        {
            switch (type)
            {
                case 1:
                    this.rdxsType1.Checked = true;
                    break;
                case 2:
                    this.rdxsType2.Checked = true;
                    break;
                case 3:
                    this.rdxsType3.Checked = true;
                    break;
                case 4:
                    this.rdxsType4.Checked = true;
                    break;
                case 5:
                    this.rdxsType5.Checked = true;
                    break;
                default:
                    this.rdxsType1.Checked = true;
                    break;
            }
        }
        private int GetYyyType()
        {
            int type = 0;

            if (this.rdyyyType1.Checked)
                type = 1;
            else if (this.rdyyyType2.Checked)
                type = 2;
            else if (this.rdyyyType3.Checked)
                type = 3;
            else if (this.rdyyyType4.Checked)
                type = 4;
            else if (this.rdyyyType5.Checked)
                type = 5;
            return type;
        }

        private void SetYyyType(int type)
        {
            switch (type)
            {
                case 1:
                    this.rdyyyType1.Checked = true;
                    break;
                case 2:
                    this.rdyyyType2.Checked = true;
                    break;
                case 3:
                    this.rdyyyType3.Checked = true;
                    break;
                case 4:
                    this.rdyyyType4.Checked = true;
                    break;
                case 5:
                    this.rdyyyType5.Checked = true;
                    break;
                default:
                    this.rdyyyType1.Checked = true;
                    break;
            }
        }

        private int GetSpdaType()
        {
            int type = 0;

            if (this.rdspType1.Checked)
                type = 1;
            else if (this.rdspType2.Checked)
                type = 2;
            else if (this.rdspType3.Checked)
                type = 3;
            else if (this.rdspType4.Checked)
                type = 4;
            else if (this.rdspType5.Checked)
                type = 5;
            return type;
        }
        private void SetSpdaType(int type)
        {
            switch (type)
            {
                case 1:
                    this.rdspType1.Checked = true;
                    break;
                case 2:
                    this.rdspType2.Checked = true;
                    break;
                case 3:
                    this.rdspType3.Checked = true;
                    break;
                case 4:
                    this.rdspType4.Checked = true;
                    break;
                case 5:
                    this.rdspType5.Checked = true;
                    break;
                default:
                    this.rdspType1.Checked = true;
                    break;
            }
        }

        private void gbtnSet3_Click(object sender, EventArgs e)
        {
            string sysName = this.ctxtSysName.Text;
            int s1 = 0;
            if (this.radio1.Checked)
                s1 = 1;
            int xs = GetXsType();
            int yyy = GetYyyType();
            int spda = GetSpdaType();
            string url = this.ctxtUrl.Text;

            string webApi = this.ctxtWebApi.Text;

            config.EmployeeRate = yyy;

            config.SalesRate = xs;
            config.SpdaRate = spda;

            config.IsChoice = s1;
            config.SysName = sysName;
            config.WebService = url;
            config.WebApi = webApi;
            // common.SetSysInfo(entity, config);
            Common2.SetConfig(config);
            MessageBox.Show("操作成功");
        }

        private void glassButton2_Click(object sender, EventArgs e)
        {
            string printName = this.comPrint.SelectedItem.ToString();
            string strNumber = this.cTextBox1.Text;
            int num = 0;


            if (printName != "" && strNumber != "")
            {
                num = int.Parse(strNumber);
                int type = 1;
                if (this.radio2.Checked)
                    type = 2;
                PrintItem model = new PrintItem();
                model.Number = num;
                model.PrintName = printName;
                model.Type = type;

              
                if (printConfig.Ls == null)
                    printConfig.Ls = new List<PrintItem>();
               
                printConfig.Ls.Add(model); 
                Common2.SetPrintConfig(printConfig);


                var entity = Common2.GetPrintConfig();
                BindPrintList(entity.Ls);
            }
            else
                Message.MsgBoxWarn("请先选择打印机");

        }


        private void gbtnSet_Click(object sender, EventArgs e)
        {
            string portName = this.cCombox1.Text;

            printConfig.PortName = portName;
            Common2.SetPrintConfig(printConfig);

            MessageBox.Show("已设置！");
        }

        private void cDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                basicBLL = new BasicBLL();
                int rowIndex = e.RowIndex;

                string name = cDataGridView1.Rows[rowIndex].Cells[0].Value.ToString();
               var model = Common2.GetPrintConfig( );
                foreach (PrintItem node in model.Ls)
                {
                    if (node.PrintName == name)//如果 找到该打印机名称
                    {
                        model.Ls.Remove(node);
                        break;
                    }
                }
                Common2.SetPrintConfig(model);
                BindPrintList(model.Ls);
                printConfig = model;
            }
            catch { }
        }

        private void cDataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
    }
}
